package cn.org.timespace.ons.inside.mina.filter.command.concrete;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.session.IoSession;

import cn.org.timespace.ons.inside.mina.filter.ESInProtocalPack;
import cn.org.timespace.ons.inside.mina.filter.command.AbstractESInCommand;
import cn.org.timespace.ons.inside.mina.util.ProcessInPackage;
import cn.org.timespace.ons.util.datatime.DateTimeUtils;
import cn.org.timespace.ons.vo.base.AppleServer;
import cn.org.timespace.ons.vo.base.DataContents;
import cn.org.timespace.ons.vo.base.Machines;


/**
 * 数据交换中心向门户上传话单信息
 * @author wind
 *
 */
public class UpdateCommand extends AbstractESInCommand {

	private final static Log log = LogFactory.getLog(UpdateCommand.class);
	private final static int numSta = 52;//信息机配置信息内容长度
	
	@Override
	public void processPackage(IoSession session, ESInProtocalPack pack) {
		ESInProtocalPack packReplay = ProcessInPackage.getPackReplay(pack);
		if(checkPackage(pack, ESInProtocalPack.SYNC_FRAME_NAME_UP)) {//消息正确
			//解析信息机配置信息
			DataContents dc = analyticalMsg(pack);
			if(dc != null && !dc.getDcid().equals("")){
				packReplay.setPackageContents("OK");
				packReplay.setCheckCodeAndPackageLength();
				log.debug("回复：" + packReplay.toString());

				session.write(packReplay.toString());
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				session.close(false);
				return;
			}
			
		}
			//出错
			packReplay.setPackageContents("ERROR");
			packReplay.setCheckCodeAndPackageLength();
			log.debug(packReplay.toString());
			session.write(packReplay.toString());
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			session.close(false);
		
	}
	
	/**
	 * 	话单信息
	 * @param pack
	 * @return
	 */
	public DataContents analyticalMsg(ESInProtocalPack pack) {
		
		String packageContents = pack.getPackageContents();//包内容
		
		DataContents dc = new DataContents();
		
		try{
			int contentLength = packageContents.length();
			if(contentLength < numSta) {//如果包内容长度不是numSta
				log.debug("包内容长度不是numSta,解析失败");
				return null;
			}
			
			//信息网关以太网接口参数(internet)
			String mid = packageContents.substring(0, 18);//36位
			String asid = packageContents.substring(18, 38);//20位
			Machines machines = ProcessInPackage.getMachine(mid);
			AppleServer appleServer = ProcessInPackage.getAppleServerById(asid);
			String sentTime = packageContents.substring(38, 52);//14位
			String contents = packageContents.substring(52, contentLength);
			
			Date sentDateTime = DateTimeUtils.getDateSql2(sentTime);
//			dc.setMid(mid);
//			dc.setAsid(asid);
			dc.setMachines(machines);
			dc.setAppleServer(appleServer);
			dc.setSentTime(sentDateTime);
			dc.setContents(contents);
			
			ProcessInPackage.addDataContents(dc);
			
			return dc;
		}
		catch(Exception e) {
			log.error("解析失败");
			log.debug(e.toString());
		}
		return null;
	}
	
}
